﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using DataAccessLayer;
using BusinessLogicLayer;

namespace ChamCong.Admin
{
    public partial class QuanLyPhuCap : BasePage
    {
        PhuCapBLL pcBLL = new PhuCapBLL();
        NhanVienBLL nvBLL = new NhanVienBLL();
        CT_PhuCapBLL ctpcBLL = new CT_PhuCapBLL();

        public static int _thang, _nam;
        //int _nam;
        int _manv;
        public static List<NHANVIEN2> lsNV;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                txtNam.Text = DateTime.Now.Year.ToString();
                drThang.SelectedValue = DateTime.Now.Month.ToString();

                string _ten = txtHoTen.Text.Trim();
                int _mapb = 0;
                _thang = int.Parse(drThang.SelectedValue);
                _nam = int.Parse(txtNam.Text);

                lsNV = nvBLL.DSNhanVien2(_mapb, _ten);

                BindBangPhuCap(lsNV, _thang, _nam);
            }

            lbTextPhuCap.Visible = false;
            lbTextMaNV.Visible = false;
            lbTextHoTen.Visible = false;
            cblPC.Visible = false;
            btnApDung.Visible = false;
        }

        protected void btnXem_Click(object sender, EventArgs e)
        {
            string _ten = txtHoTen.Text.Trim();
            int _mapb = int.Parse(drPhongBan.SelectedValue);
            _thang = int.Parse(drThang.SelectedValue);
            _nam = int.Parse(txtNam.Text);

            tbCTPhuCap.Visible = false;

            lsNV = nvBLL.DSNhanVien2(_mapb, _ten);

            BindBangPhuCap(lsNV, _thang, _nam);
        }

        protected void btnXemTatCa_Click(object sender, EventArgs e)
        {
            string _ten = txtHoTen.Text.Trim();
            int _mapb = 0;
            _thang = int.Parse(drThang.SelectedValue);
            _nam = int.Parse(txtNam.Text);

            tbCTPhuCap.Visible = false;

            lsNV = nvBLL.DSNhanVien2(_mapb, _ten);

            BindBangPhuCap(lsNV, _thang, _nam);
        }

        protected void BindBangPhuCap(List<NHANVIEN2> lsNV, int thang, int nam)
        {
            var bangPHUCAP = new DataTable("BangPHUCAP");

            bangPHUCAP.Columns.Add("ID", typeof(string));

            bangPHUCAP.Columns.Add("Họ Tên");

            List<PHUCAP> lsPC = pcBLL.LayTatCa();

            foreach (var item in lsPC)
            {
                bangPHUCAP.Columns.Add(item.TenPhuCap.ToString());
                //bangPHUCAP.Columns.Add(item.TenPhuCap.ToString(), typeof(Image));
            }

            foreach (var nv in lsNV)
            {
                var dataRow = bangPHUCAP.NewRow();

                int _thang = int.Parse(drThang.SelectedValue);
                int _nam = int.Parse(txtNam.Text);
                //======================Add dữ liệu vào GridView============================
                dataRow["ID"] = nv.MaNV.ToString();
                dataRow["Họ Tên"] = nv.HoTen.ToString();

                List<CTPHUCAP> lsctPC = ctpcBLL.LayDSPCThang(_thang, _nam);
                foreach (var item in lsPC)
                {
                    foreach (var pc in lsctPC)
                    {
                        if (item.MaPhuCap == pc.MaPhuCap && pc.MaNV == nv.MaNV)
                        {
                            dataRow[item.TenPhuCap.ToString()] = "X";
                            Image a = new Image();
                            a.ImageUrl = "./images/calendar_button.png";
                            goto xong;
                        }
                    }

                    dataRow[item.TenPhuCap.ToString()] = "";
                xong: { }
                }
                //=============================================================================
                bangPHUCAP.Rows.Add(dataRow);
            }
            //Binding time sheet table with data grid view
            dgvPhuCap.DataSource = bangPHUCAP;
            dgvPhuCap.DataBind();
        }

        protected void dgvPhuCap_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {

                Button btnSua = (Button)e.Row.FindControl("btnSua");
                if (btnSua != null)
                {
                    btnSua.Attributes["ID"] = e.Row.Cells[1].Text;
                }
            }

            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                for (int columnIndex = 3; columnIndex < e.Row.Cells.Count; columnIndex++)
                {
                    e.Row.Cells[columnIndex].Width = Unit.Pixel(40);
                    e.Row.Cells[columnIndex].HorizontalAlign = HorizontalAlign.Center;
                }
            }
        }

        protected void btnSua_Click(object sender, EventArgs e)
        {
            tbCTPhuCap.Visible = true;
            lbTextMaNV.Visible = true;
            lbTextHoTen.Visible = true;
            cblPC.Visible = true;
            btnApDung.Visible = true;
            for (int i = 0; i < cblPC.Items.Count; i++)
            {
                cblPC.Items[i].Selected = false;
            }
            Button btn = (Button)sender;
            _manv = int.Parse(btn.Attributes["ID"]);
            //lbThongbao.Text = "Đã Click vô Link Button!" + _manv.ToString();

            int _thang = int.Parse(drThang.SelectedValue);
            int _nam = int.Parse(txtNam.Text);

            lbMaSo.Text = _manv.ToString();
            lbHoTen.Text = nvBLL.LayNhanVien(_manv).HoTen.ToString();

            CT_PhuCapBLL ctpcBLL = new CT_PhuCapBLL();
            List<int> lsctPC = ctpcBLL.LayPCThang(_manv, _thang, _nam);

            List<PHUCAP> lsPC = pcBLL.LayTatCa();
            for (int i = 0; i < cblPC.Items.Count; i++)
            {
                foreach (int a in lsctPC)
                {
                    if (int.Parse(cblPC.Items[i].Value) == a)
                    {
                        cblPC.Items[i].Selected = true;
                    }
                }
            }
        }

        protected void btnApDung_Click(object sender, EventArgs e)
        {
            _thang = int.Parse(drThang.SelectedValue);
            _nam = int.Parse(txtNam.Text);

            _manv = int.Parse(lbMaSo.Text);
            ctpcBLL.XoaPC(_manv, _thang, _nam);
            //Thêm mới
            for (int i = 0; i < cblPC.Items.Count; i++)
            {
                if (cblPC.Items[i].Selected == true)
                {
                    int _mapc = int.Parse(cblPC.Items[i].Value);
                    CT_PHUCAP pc = new CT_PHUCAP();
                    pc.MaPhuCap = int.Parse(cblPC.Items[i].Value);
                    pc.Nam = _nam;
                    pc.Thang = _thang;
                    pc.MaNV = _manv;
                    ctpcBLL.Them(pc);
                }
            }
            
            lbTextMaNV.Visible = false;
            lbTextHoTen.Visible = false;
            cblPC.Visible = false;
            btnApDung.Visible = false;
            lbHoTen.Visible = false;
            lbMaSo.Visible = false;
            tbCTPhuCap.Visible = false;
            sbcMsgBox.SetTitleText(SBWebControls.sbcMsgBox.eMessageType.Success, "Thông báo", "Chỉnh sửa thành công!");
            BindBangPhuCap(lsNV, _thang, _nam);
        }

        protected void dgvPhuCap_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            dgvPhuCap.PageIndex = e.NewPageIndex;
            this.BindBangPhuCap(lsNV, _thang, _nam);
        }
    }
}